package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Supplier;
import com.test.ws.s1.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /// 查询
    @RequestMapping("/supplierCha")
    public String selectSupplier(Supplier supplier, HttpServletRequest request,
                                 @RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "3") int pageSize) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        System.out.println("================"+supplier);
        ArrayList<Supplier> suppliers = supplierService.selectSupplier(supplier);
        PageInfo pageInfo = new PageInfo(suppliers);
        System.out.println(suppliers);
        request.setAttribute("supplierCha",suppliers);
        request.setAttribute("pageInfo",pageInfo);
        return "houtai/supplier";
    }

    /// 进入添加页面
    @RequestMapping("/supplierAddYeMian")
    public String supplierAdd(HttpServletRequest request) {
        return "houtai/supplierAddYeMian";
    }

    /// 添加
    @RequestMapping("/supplierAdd")
    public String insertSupplier(Supplier supplier, HttpServletRequest request) {
        int i = supplierService.insertSupplier(supplier);
        System.out.println(i+"=============="+supplier);
        return "redirect:/supplierCha";
    }

    /// 删除
    @RequestMapping("/supplierDel")
    public String deleteSupplier(Supplier supplier, HttpServletRequest request) {
        String id = request.getParameter("id");
        supplier.setSupplier_id(Integer.parseInt(id));
        int i = supplierService.deleteSupplier(supplier);
        System.out.println(i);
        return "redirect:/supplierCha";
    }

    /// 回显
    @RequestMapping("/selectOneSupplier")
    public String selectOneSupplier(Supplier supplier, HttpServletRequest request) {
        String id = request.getParameter("id");
        supplier.setSupplier_id(Integer.parseInt(id));
        Supplier supplier1 = supplierService.selectOneSupplier(supplier);
        request.setAttribute("sjhx", supplier1);
        return "houtai/supplierHuiXian";
    }

    /// 修改
    @RequestMapping("/supplierGai")
    public String updateSupplier(Supplier supplier, HttpServletRequest request) {
        System.out.println("================+++++++++++================="+supplier);
        int i = supplierService.updateSupplier(supplier);
        System.out.println(i);
        return "redirect:/supplierCha";
    }

}
